from ..dtos.news import NewsDTO, NewsListDTO
from ..exceptions import ValidationException
from ..models.news import NewsItem
from ..repositories.news import NewsRepository


class NewsService:
    def __init__(self):
        self.repository = NewsRepository()

    def create_news(self, data: dict) -> NewsDTO:
        required_fields = ['title', 'content']
        for field in required_fields:
            if field not in data or not data[field]:
                raise ValidationException(f"{field} is required")

        news = self.repository.create({
            'news_item_title': data['title'],
            'news_item_content': data['content'],
            'news_item_content_div': data.get('content_div'),
            'news_item_subtitle': data.get('subtitle'),
            'news_item_cate': data.get('category', 'general'),
            'status': data.get('status', 1),
            'image_list': data.get('image_list'),
            'video_list': data.get('video_list'),
            'audio_list': data.get('audio_list'),
            'news_item_editor': data.get('editor'),
            'news_item_link': data.get('link'),
            'news_item_src': data.get('source'),
            'news_item_reldate': data.get('release_date'),
            'news_item_origin_id': data.get('origin_id')
        })
        return self._to_dto(news)

    def get_news(self, news_id: int) -> NewsDTO:
        news = self.repository.get_by_id(news_id)
        return self._to_dto(news)

    def _to_dto(self, news: NewsItem) -> NewsDTO:
        return NewsDTO(
            id=news.news_item_id,
            title=news.news_item_title,
            subtitle=news.news_item_subtitle,
            content=news.news_item_content,
            content_div=news.news_item_content_div,
            category=news.news_item_cate,
            status=news.status,
            created_at=news.create_time,
            updated_at=news.update_time,
            image_list=news.image_list,
            video_list=news.video_list,
            audio_list=news.audio_list,
            editor=news.news_item_editor,
            link=news.news_item_link,
            source=news.news_item_src,
            release_date=news.news_item_reldate,
            origin_id=news.news_item_origin_id
        )

    def update_news(self, news_id: int, data: dict) -> NewsDTO:
        news = self.repository.update(news_id, {
            'news_item_title': data.get('title'),
            'news_item_subtitle': data.get('subtitle'),
            'news_item_content': data.get('content'),
            'news_item_content_div': data.get('content_div'),
            'news_item_cate': data.get('category'),
            'status': data.get('status'),
            'image_list': data.get('image_list'),
            'video_list': data.get('video_list'),
            'audio_list': data.get('audio_list'),
            'news_item_editor': data.get('editor'),
            'news_item_link': data.get('link'),
            'news_item_src': data.get('source'),
            'news_item_reldate': data.get('release_date'),
            'news_item_origin_id': data.get('origin_id')
        })
        return self._to_dto(news)

    def delete_news(self, news_id: int) -> bool:
        return self.repository.delete(news_id)

    def list_news_by_category(self, category: str, page: int = 1, page_size: int = 10) -> NewsListDTO:
        queryset = self.repository.model_class.objects.filter(
            news_item_cate=category
        ).order_by('-create_time')
        
        total = queryset.count()
        items = queryset[(page-1)*page_size : page*page_size]
        
        return NewsListDTO(
            items=[self._to_dto(item) for item in items],
            total=total,
            page=page,
            page_size=page_size
        )